package com.example.test.controller;

import com.example.test.entity.Notice;
import com.example.test.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("api/notices")
@CrossOrigin
public class NoticeController {
    @Autowired
    private NoticeService noticeService;
    @GetMapping
    public ResponseEntity<List<Notice>> getAllNotices() {
        List<Notice> notices = noticeService.getAllNotices();
        return ResponseEntity.ok(notices);
    }
    @PutMapping("/{id}")
    public ResponseEntity<Notice> updateNotice(  @PathVariable int id,
                                                 @RequestParam("text") String text,
                                                 @RequestParam("ifplay") int ifplay
                                               ) throws IOException {
        Notice notice=new Notice();
        notice.setText(text);
        notice.setIfplay(ifplay);
        return ResponseEntity.ok(noticeService.updateNotice(id, notice));
    }
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteNotice(@PathVariable int id) {
        noticeService.deleteNotice(id);
        return ResponseEntity.noContent().build();
    }
}
